﻿using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.Report;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Report;

namespace DataObjects.ADO.SqlServer.Report
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlServerReportGroupDao : IReportGroupDao
    {

        #region IReportGroup Members

        /// <summary>
        /// Gets the report group.
        /// </summary>
        /// <param name="reportGroupId">The report group identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public ReportGroup GetReportGroup(long reportGroupId)
        {
            const string sql = @"uspGet_ReportGroup_ByID";
            object[] parms = { "@ReportGroupId", reportGroupId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }
        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<ReportGroup> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_ReportGroup";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_ReportGroup";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }


        /// <summary>
        /// Gets the report groups.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<ReportGroup> GetReportGroups()
        {
            const string sql = @"uspGetAll_ReportGroup";
            return Db.ReadList(sql, CommandType.StoredProcedure, Make);
        }

        /// <summary>
        /// Inserts the report group.
        /// </summary>
        /// <param name="reportGroup">The report group.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public string InsertReportGroup(ReportGroup reportGroup)
        {
            const string sql = @"uspInsert_ReportGroup";
            return Db.Insert(sql, Take(reportGroup));
            //reportGroup.Version = GetReportGroup(reportGroup.ReportGroupId).Version;
        }

        /// <summary>
        /// Updates the report group.
        /// </summary>
        /// <param name="reportGroup">The report group.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public string UpdateReportGroup(ReportGroup reportGroup)
        {
            const string sql = @"uspUpdate_ReportGroup";
            return Db.Update(sql, CommandType.StoredProcedure, Take(reportGroup));
        }

        /// <summary>
        /// Deletes the report group.
        /// </summary>
        /// <param name="reportGroup">The report group.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public string DeleteReportGroup(ReportGroup reportGroup)
        {
            const string sql = @"uspDelete_ReportGroup";
            return Db.Update(sql, CommandType.StoredProcedure, Take(reportGroup));
        }

        #endregion

        /// <summary>
        /// Creates a ReportGroup object based on DataReader.
        /// </summary>
        private static readonly Func<IDataReader, ReportGroup> Make = reader =>
           new ReportGroup
           {
               ReportGroupId = reader["ReportGroupId"].AsId(),
               IsTypical = reader["IsTypical"].AsBool(),
               Description = reader["Description"].AsString(),
               IsVoucher = reader["IsVoucher"].AsBool(),
               Name = reader["Name"].AsString(),
               IsActive = reader["IsActive"].AsBool()
           };

        /// <summary>
        /// Creates query parameters list from ReportGroup object
        /// </summary>
        /// <param name="reportGroup"></param>
        /// <returns>Name value parameter list.</returns>
        private static object[] Take(ReportGroup reportGroup)
        {
            return new object[]  
            {
                "@ReportGroupId", reportGroup.ReportGroupId,
                "@IsTypical", reportGroup.IsTypical,
                "@Description", reportGroup.Description,
                "@IsVoucher", reportGroup.IsVoucher,
                "@Name", reportGroup.Name,
                "@IsActive", reportGroup.IsActive
            };
        }
    }
}
